package com.workspace.js_website.controller;

import com.workspace.js_website.bean.RespBean;
import com.workspace.js_website.bean.User;
import com.workspace.js_website.bean.ValidateCode;
import com.workspace.js_website.bean.MetaBean;
import com.workspace.js_website.service.SmsCodeService;
import com.workspace.js_website.service.UserService;
import com.workspace.js_website.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录注册相关控制
 */

@CrossOrigin
@RestController
public class LoginRegisterController {

    private final UserService userService;
    private final SmsCodeService smsCodeService;

    @Autowired
    public LoginRegisterController(UserService userService, SmsCodeService smsCodeService) {
        this.userService = userService;
        this.smsCodeService = smsCodeService;
    }

    @RequestMapping("/login_page")
    public RespBean loginPage() {
        MetaBean meta = new MetaBean(400,"尚未登录，请登录!");
        return new RespBean("", meta);
    }

    @PostMapping("/register")
    public RespBean register(HttpServletRequest request) throws ServletRequestBindingException {
        //通过HttpServletRequest获取参数时前端需要使用x-www-form-urlencoded格式,
        //通过@RequestBody获取参数时前端需要使用application/json格式的参数,
        //不然会出现获取不到数据或者415报错的情况
        User user = new User();
        user.setMobile(ServletRequestUtils.getStringParameter(request, "mobile"));
        user.setUsername(ServletRequestUtils.getStringParameter(request, "username"));
        user.setPassword(ServletRequestUtils.getStringParameter(request, "password"));
        user.setName(ServletRequestUtils.getStringParameter(request, "name"));
        user.setCompany(ServletRequestUtils.getStringParameter(request, "company"));
        user.setDepartment(ServletRequestUtils.getStringParameter(request, "department"));
        user.setEmail(ServletRequestUtils.getStringParameter(request, "email"));
        return userService.register(user);
    }

    @PostMapping("/findBackPassword")
    public RespBean findBackPassword(HttpServletRequest request) throws ServletRequestBindingException {
        String mobile = ServletRequestUtils.getStringParameter(request, "mobile");
        String newPassword = ServletRequestUtils.getStringParameter(request, "newPassword");
        return userService.findBackPassword(mobile, newPassword);
    }

    @PostMapping("/updatePassword")
    public RespBean updatePassword(HttpServletRequest request) throws ServletRequestBindingException {
        User currentUser = Util.getCurrentUser();
        String oldPassword = ServletRequestUtils.getStringParameter(request, "oldPassword");
        String newPassword = ServletRequestUtils.getStringParameter(request, "newPassword");
        return userService.updatePassword(currentUser, oldPassword, newPassword);
    }

    @GetMapping("/sendSmsCode/{target}")
    public RespBean sendSmsCode(@PathVariable("target") String target, HttpServletRequest request) throws ServletRequestBindingException {
        //生成一个验证码
        ValidateCode smsCode = smsCodeService.generate();
        //从请求中获取手机号
        String mobile = ServletRequestUtils.getStringParameter(request, "mobile");
        //把验证码和手机号写入session中
        request.getSession().setAttribute("code", smsCode);
        request.getSession().setAttribute("mobile", mobile);
        return smsCodeService.send(mobile, target, smsCode.getCode());
    }

//    @GetMapping("/sendSmsCode/{target}")
//    public String sendSmsCode(@PathVariable("target") String target, HttpServletRequest request) throws ServletRequestBindingException {
//        ValidateCode smsCode = smsCodeService.generate();
//        smsCode.setCode("111");
//        String mobile = ServletRequestUtils.getStringParameter(request, "mobile");
//        request.getSession().setAttribute("code", smsCode);
//        request.getSession().setAttribute("mobile", mobile);
//        return smsCode.getCode();
//    }
}
